package com.pet.shop.utils;

import com.pet.shop.dto.PageDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaochanzi on 2018/3/15.
 */
public class ApiResultUtil {

    public static Map<String, Object> success(Object data){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", HttpStatus.OK.value());
        if(data != null) map.put("data", data);
        map.put("msg", "");
        return map;
    }

    public static Map<String, Object> success_data(Object data){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", data == null? HttpStatus.NOT_FOUND.value() : HttpStatus.OK.value());
        if(data != null) map.put("data", data);
        map.put("msg", data == null? "未找到数据" : "");
        return map;
    }

    public static Map<String, Object> success_page(PageDto<?> page){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", HttpStatus.OK.value());
        map.put("total", page.getTotalRecord());
        map.put("data", page.getResults());
        map.put("msg", "");
        return map;
    }

    public static Map<String, Object> success_dataTables_page(PageDto<?> page){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", HttpStatus.OK.value());
        map.put("recordsTotal", page.getTotalRecord());
        map.put("recordsFiltered", page.getTotalRecord());
        map.put("data", page.getResults());
        map.put("msg", "");
        return map;
    }

    public static <T extends Serializable> PageDto<T> createPage(HttpServletRequest request, Class<T> cls){
        PageDto<T> page = new PageDto<T>();
        String pageNum = request.getParameter("page");
        String rows = request.getParameter("rows");
        page.setPage(StringUtils.isEmpty(pageNum)? 1 : Integer.parseInt(pageNum));
        page.setRows(StringUtils.isEmpty(rows)? 1 : Integer.parseInt(rows));
        //参数
        Enumeration<?> es = request.getParameterNames();
        while (es.hasMoreElements()) {
            String name = (String) es.nextElement();
            String value = request.getParameter(name);
            if(value == null || value.equals("")) continue;
            if(name.startsWith("search")) page.getParams().put(name.substring(7), value);
            else if(name.startsWith("order")) page.getOrderBy().put(name.substring(6), value);
        }
        return page;
    }

    /**
     * 参数错误
     * @param msg
     * @return
     */
    public static Map<String, Object> error_param(String msg){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", HttpStatus.BAD_REQUEST.value());
        map.put("msg", msg);
        return map;
    }

    /**
     * 服务器错误
     * @param msg
     * @return
     */
    public static Map<String, Object> error_server(String msg){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
        map.put("msg", msg);
        return map;
    }
}
